// pages/inform/inform.js
import {
  noticeList,
  isRead,
  infoGoodsDetail
} from '../../api.js'
let app = getApp();
Page({
  data: {
    readList: [], //未读通知
    readedList: [], //已读通知
    hasReadList:true, //默认不显示未读空白页
    hasReadedList:true, //默认不显示已读空白页
    read_page: 1,
    read_last_page: 1,
    readed_page: 1,
    readed_last_page: 1,
    isRead: true,
    isSkip:false, //判断是否跳转过页面
  },

  /**
   * 生命周期函数--监听页面加载
   */
 
  onLoad: function(options) {
    this.informList(1)
    this.informList2(1)
  },
  onShow:function(){
    console.log('onshow')
    if (this.data.isSkip==true){
      if (this.data.readList.length == 0 && this.data.isRead == true) {
        this.setData({
          hasReadList: false
        })
      }
      if (this.data.readedList.length == 0 && this.data.isRead == false) {
        this.setData({
          hasReadedList: false
        })
      }
    }
 
  },
  //请求数据(未读)
  informList(page) {
    const _this = this;
    wx.showLoading({
      title: '加载中',
    })
    _this.setData({
      hasReadList:true
    })
    noticeList({
      token: app.globalData.token,
      page,
      singlepage: 20,
      is_read: 0
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        var data = res.data.data;
        _this.setData({
          readList: data.data,
          read_last_page: data.last_page,
          read_page: data.current_page
        })
        wx.hideLoading();
        if(data.data.length==0){
          _this.setData({
            hasReadList:false
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  //请求数据(已读)
  informList2(page) {
    const _this = this;
    wx.showLoading({
      title: '加载中',
    })
    _this.setData({
      hasReadedList:true
    })
    noticeList({
      token: app.globalData.token,
      page,
      singlepage: 20,
      is_read: 1
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        var data = res.data.data
        _this.setData({
          readedList: data.data,
          readed_last_page: data.last_page,
          readed_page: data.current_page
        })
        wx.hideLoading();
        if(data.data.length==0){
          _this.setData({
            hasReadedList:false
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }

    })
  },
  changeInfo(e) {
    console.log(e)
    this.setData({
      isRead: (e.currentTarget.dataset.info == 1) ? true : false
    })
    this.informList(1)
    this.informList2(1)

  },
  /* 页面上拉触底事件的处理函数 */
  onReachBottom: function() {
    if (this.data.isRead == true) {
      if (this.data.read_page < this.data.read_last_page) {
        this.pullRequest(this.data.read_page + 1)
      } else {
        wx.showToast({
          title: '没有更多数据啦',
          icon: 'none',
        })
      }
    } else {
      if (this.data.readed_page < this.data.readed_last_page) {
        this.pullRequest2(this.data.readed_page + 1)
      } else {
        wx.showToast({
          title: '没有更多数据啦',
          icon: 'none',
        })
      }
    }

  },

  //上拉请求数据(未读)
  pullRequest(page) {
    const _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    noticeList({
      token: app.globalData.token,
      page,
      singlepage: 20,
      is_read: 0
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        var data = res.data.data;

        _this.setData({
          readList: _this.data.readList.concat(data.data),
          read_last_page: data.last_page,
          read_page: data.current_page
        })
        wx.hideLoading();
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  //上拉请求数据(已读)
  pullRequest2(page) {
    const _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    noticeList({
      token: app.globalData.token,
      page,
      singlepage: 20,
      is_read: 1
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        var data = res.data.data;
        _this.setData({
          readedList: _this.data.readedList.concat(data.data),
          readed_last_page: data.last_page,
          readed_page: data.current_page
        })
        wx.hideLoading();
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  //跳转页面
  detailSkip(e) {
    console.log('aaa', e)
    let _this=this;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let sid = e.currentTarget.dataset.sid;
    let info_batchcode = e.currentTarget.dataset.info_batchcode;
    let con = e.currentTarget.dataset.con;
    _this.setData({
      isSkip:true
    })
    isRead({
      token: app.globalData.token,
      id
    }).then(res => {
  console.log('res',res)
      if (res.data.code == 666 || res.data.code==30068) {
        infoGoodsDetail({
          token: app.globalData.token,
          id: sid
        }).then(res => {
          if(res.data.code==666){
            if (_this.data.isRead==true){
              _this.data.readList.splice(index, 1)
              setTimeout(function () {
                _this.setData({
                  readList: _this.data.readList
                })
              }, 1500)
            }
            if (type == 1) { //跳转到自有商品详情
              wx.navigateTo({
                url: '/pages/addGood/addGood?fuc=look&id=' + sid,
              })
            }
            if (type == 2) { //跳转到门店订单详情
              wx.navigateTo({
                url: '/pages/shopOrderControl/orderDetail/orderDetail?type=1&info_batchcode=' + info_batchcode,
              })
            }
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          }else {
            wx.showToast({
              title: `${con}已删除`,
              icon: 'none',
            })
            _this.data.readList.splice(index, 1)
            setTimeout(function(){
              _this.setData({
                readList: _this.data.readList
              })
            },1500)
            return
          }

        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }

    })
  },
})